package institution

import (
	"net/http"

	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/EDDYCJY/go-gin-example/pkg/util"
	"github.com/EDDYCJY/go-gin-example/service/institution_service"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

type GetSubAccountVerify struct {
}

func GetSubAccount(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form GetSubAccountVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.GetSubAccount{
		InstitutionID: InstitutionID,
		PageNum:       util.GetPage(c),
		PageSize:      util.GetLimit(c),
	}
	data, err := Service.GetSubAccount()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type AddSubAccountVerify struct {
	UserName     string `alias:"用户名" form:"user_name"  json:"user_name" valid:"Required;"`
	UserPassword string `alias:"密码" form:"user_password"  json:"user_password" valid:"Required;"`
}

func AddSubAccount(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddSubAccountVerify
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	UserID := c.MustGet("user_id").(int)

	Service := institution_service.AddSubAccount{
		InstitutionID: InstitutionID,
		UserID:        UserID,
		UserName:      form.UserName,
		UserPassword:  form.UserPassword,
	}
	data, err := Service.AddSubAccount()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type EditSubAccountVerify struct {
	ID           int    `form:"id" valid:"Required;"`
	UserPassword string `alias:"密码" form:"user_password"  json:"user_password" valid:"Required;"`
}

func EditSubAccount(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = EditSubAccountVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.EditSubAccount{
		UserID:        form.ID,
		InstitutionID: InstitutionID,
		UserPassword:  form.UserPassword,
	}
	data, err := Service.EditSubAccount()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}

type DeleteSubAccountVerify struct {
	ID int `form:"id" valid:"Required;"`
}

func DeleteSubAccount(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form = DeleteSubAccountVerify{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	_, errCode := app.BindAndValidMsg(c, &form)
	if errCode != nil {
		appG.ResponseMsg(errCode, nil)
		return
	}

	InstitutionID := c.MustGet("institution_id").(int)
	Service := institution_service.DeleteSubAccount{
		UserID:        form.ID,
		InstitutionID: InstitutionID,
	}
	data, err := Service.DeleteSubAccount()
	if err != nil {
		appG.ResponseMsg(err, nil)
		return
	}
	//异步日志
	appG.Response(http.StatusOK, e.SUCCESS, data)

}
